INC_all_loaded_imgs = {}
INC_all_loaded_fonts = {}
INC_fontslots = {} 
INC_all_loaded_colors = {}
INC_startdrawing = 0
INC_standard_fontsize = 11
INC_buffer_active = 0
INC_font = "verdana.ttf"
INC_printfont = 0
INC_newtex_w = 0
INC_newtex_h = 0
INC_libdir = pge.dir.getcwd()

Controls = {}
screen = {}
Font = {}
Color = {}
Image = {}
System = {}
--io = {}

buffer = pge.texture.create(480,272)

--##additional functions

function INC_check_color(color)
r = pge.gfx.red(color)
g = pge.gfx.green(color)
b = pge.gfx.blue(color)
a = pge.gfx.alpha(color)
found = false
if #INC_all_loaded_colors ~= 0 then 
        for i = 1,#INC_all_loaded_colors do 
                if not found then 
        check_r = pge.gfx.red(INC_all_loaded_colors[i])
        check_g = pge.gfx.green(INC_all_loaded_colors[i])
        check_b = pge.gfx.blue(INC_all_loaded_colors[i])
        check_a = pge.gfx.alpha(INC_all_loaded_colors[i])
                if r == check_r and g == check_g and b == check_b and a == check_a then 
                found = i
                end     
        end 
        end 
end 
return found
end

function INC_set_buffer()
	if INC_buffer_active == 0 then
		buffer:tovram()
		pge.texture.settarget(buffer)
		INC_buffer_active = 1
	end
end

function INC_unset_buffer()
	if INC_buffer_active == 1 then
		--pge.gfx.rendertoscreen()
		INC_buffer_active = 0
	end
end

function INC_move_buffer()
	buffer:toram()
end

function INC_drawing_enabled()
	if INC_startdrawing == 0 then
		pge.gfx.startdrawing()
		INC_startdrawing = 1
	end
end

--##CONTROLS
function Controls.read()
pgepad = {}
pge.controls.update()
    function pgepad:cross()
        if pge.controls.held(PGE_CTRL_CROSS) then 
        return true 
        end 
    end 
    function pgepad:square()
        if pge.controls.held(PGE_CTRL_SQUARE) then 
        return true 
        end 
    end 
    function pgepad:circle()
        if pge.controls.held(PGE_CTRL_CIRCLE) then 
        return true 
        end 
    end     
    function pgepad:triangle()
        if pge.controls.held(PGE_CTRL_TRIANGLE) then 
        return true 
        end 
    end     
    function pgepad:start()
        if pge.controls.held(PGE_CTRL_START) then 
        return true 
        end 
    end     
    function pgepad:select()
        if pge.controls.held(PGE_CTRL_SELECT) then 
        return true 
        end 
    end     
    function pgepad:up()
        if pge.controls.held(PGE_CTRL_UP) then 
        return true 
        end 
    end     
    function pgepad:down()
        if pge.controls.held(PGE_CTRL_DOWN) then 
        return true 
        end 
    end 
    function pgepad:left()
        if pge.controls.held(PGE_CTRL_LEFT) then 
        return true 
        end 
    end     
    function pgepad:right()
        if pge.controls.held(PGE_CTRL_RIGHT) then 
        return true 
        end 
    end 
    function pgepad:r()
        if pge.controls.held(PGE_CTRL_RTRIGGER) then 
        return true 
        end 
    end 
    function pgepad:l()
        if pge.controls.held(PGE_CTRL_LTRIGGER) then 
        return true 
        end 
    end     
	function pgepad:analogX()
		return pge.controls.analogx()
	end
	function pgepad:analogY()
		return pge.controls.analogy()
	end
    function pgepad:home()
    return false
    end
    function pgepad:note()
    return false
    end     
    function pgepad:screen()
    return false
    end     
    function pgepad:hold()
    return false
    end 
    function pgepad:wlan()
	    return pge.net.switchstatus()
    end    
return pgepad
end 

function Controls.wlan()
	return pge.net.switchstatus()
end

--##COLOR
function Color.new(r,g,b,a)
        if not a then
                a = 255
        end
        pre_pos = INC_check_color(pge.gfx.createcolor(r,g,b,a))
       if pre_pos then 
          num = pre_pos
          INC_color_table = {id =  pre_pos}
        else
        INC_all_loaded_colors[#INC_all_loaded_colors+1] = pge.gfx.createcolor(r,g,b,a)
          num = #INC_all_loaded_colors
          INC_color_table = {id = #INC_all_loaded_colors}
      end 
        
        function INC_color_table:colors()
                return {r = pge.gfx.red(INC_all_loaded_colors[num]),g = pge.gfx.green(INC_all_loaded_colors[num]),b = pge.gfx.blue(INC_all_loaded_colors[num]),a =pge.gfx.alpha(INC_all_loaded_colors[num])}
        end
return INC_color_table
end

--##SCREEN
function screen.flip()
        INC_drawing_enabled()
		INC_unset_buffer()
        pge.gfx.rendertoscreen()
          buffer:activate()
          buffer:draw(0,0)
        pge.gfx.enddrawing()
        INC_startdrawing = 0
		pge.gfx.swapbuffers()
		collectgarbage()
end
 
function screen.waitVblankStart(wait)
        if wait then
                pge.delay(wait*100)
        end
end
 
function screen:clear(color)
        if not color then
        color = Color.new(0,0,0)
        end
        INC_drawing_enabled()
        INC_set_buffer()
        pge.gfx.clearscreen(INC_all_loaded_colors[color.id])
end
 
function screen:save(path)
        pge.gfx.screenshot(path)
end
 
function screen:drawLine(x0, y0, x1, y1, color)
        INC_drawing_enabled()
		INC_set_buffer()
        pge.gfx.drawline(x0, y0, x1, y1, INC_all_loaded_colors[color.id])
end
 
function screen:print(x ,y ,text,color)
        if INC_printfont == 0 then
				INC_all_loaded_fonts[#INC_all_loaded_fonts+1] = pge.font.load(INC_font,INC_standard_fontsize, PGE_RAM)
				INC_printfont = #INC_all_loaded_fonts
        end
        INC_drawing_enabled()
		INC_set_buffer()
        pge.font.activate(INC_all_loaded_fonts[INC_printfont])
        pge.font.print(INC_all_loaded_fonts[INC_printfont], x, y, INC_all_loaded_colors[color.id], text)
end
 
function screen:blit(x,y,img,sourcex, sourcey, width, height, alpha)
		if INC_all_loaded_imgs[img.id] ~= "empty" then
			if not sourcex then
					sourcex = 0
			end
			if not sourcey then
					sourcey = 0
			end
			if not width then
					width = INC_all_loaded_imgs[img.id]:width()
			end
			if not height then
					height = INC_all_loaded_imgs[img.id]:height()
			end
			if not alpha then
					alpha = false
			end
			INC_drawing_enabled()
			INC_set_buffer()
			INC_all_loaded_imgs[img.id]:activate()
			INC_all_loaded_imgs[img.id]:draw(x, y, INC_all_loaded_imgs[img.id]:width(), INC_all_loaded_imgs[img.id]:height(), sourcex, sourcey, width, height)
		end
end
 
function screen:fontPrint(font, x, y, text, color)
        INC_drawing_enabled()
		INC_set_buffer()
        pge.font.activate(INC_all_loaded_fonts[font.id])
        pge.font.print(INC_all_loaded_fonts[font.id], x, y, INC_all_loaded_colors[color.id], text)
end
 
function screen:fillRect(x, y, width, height, color)
        INC_drawing_enabled()
		INC_set_buffer()
        pge.gfx.drawrect(x, y, width, height, INC_all_loaded_colors[color.id])
end
 
 
function screen:width()
        return 480
end
 
function screen:height()
        return 272
end
 
function screen:pixel(x, y , color)
        INC_drawing_enabled()
        if not color then
                PXpgecolor = pge.texture.pixel(buffer, x, y)
                PXcolor = Color.new(pge.gfx.red(PXpgecolor),pge.gfx.green(PXpgecolor),pge.gfx.blue(PXpgecolor),pge.gfx.alpha(PXpgecolor))
        return PXcolor
        else
			pge.texture.pixel(buffer,x,y,INC_all_loaded_colors[color.id])
        end
end

--##FONT

function Font.load(file, slot)
	if not slot then
		INC_all_loaded_fonts[#INC_all_loaded_fonts+1] = pge.font.load(file, INC_standard_fontsize, PGE_RAM)
		local font_table = {id = #INC_all_loaded_fonts}
		local num = #INC_all_loaded_fonts
		function font_table:setPixelSizes(width, height)
			if not height then height = width*1.25 end
			INC_all_loaded_fonts[num] = pge.font.load(file, height)
		end
		function font_table:getTextSize(_string)
			return pge.font.measure(INC_all_loaded_fonts[num], _string)
		end
		return font_table
	else
		INC_all_loaded_fonts[#INC_all_loaded_fonts+1] = pge.font.load(file, INC_standard_fontsize, PGE_RAM)
		INC_fontslots[slot] = #INC_all_loaded_fonts
	end
end

function Font.init()
	return true
end

function Font.print(x,y,text,color,fontslot) 
	INC_drawing_enabled()
	INC_set_buffer()
    pge.font.activate(INC_all_loaded_fonts[INC_fontslots[fontslot]])
    pge.font.print(INC_all_loaded_fonts[INC_fontslots[fontslot]], x, y, INC_all_loaded_colors[color.id], text)
end

function Font.unload(slot)
	INC_all_loaded_fonts[INC_fontslots[slot]] = nil
	INC_fontslots[slot] = nil
end

function Font.term()
	return true
end

function Font.color(r,g,b,a)
	return true
end

--##IMAGE
function Image.load(path)
	if path == "INC_NEW_TEX" then 
		INC_all_loaded_imgs[#INC_all_loaded_imgs+1] = pge.texture.create(INC_newtex_w,INC_newtex_h)
	else
		if string.sub(path, -4) == ".png" then
			INC_all_loaded_imgs[#INC_all_loaded_imgs+1] = pge.texture.load(path, PGE_RAM)
		else
			INC_all_loaded_imgs[#INC_all_loaded_imgs+1] = "empty"
		end
	end 
	local imgnum = #INC_all_loaded_imgs
	local img_table = {id = #INC_all_loaded_imgs}
	function img_table:pixel(x,y,color)
		if INC_all_loaded_imgs[imgnum] ~= "empty" then
			INC_drawing_enabled()
			if not color then
				PXcolor = pge.texture.pixel(INC_all_loaded_imgs[imgnum], x, y)
				return PXcolor
			else
				pge.texture.pixel(INC_all_loaded_imgs[imgnum], x, y, INC_all_loaded_colors[color.id])
			end
		end
    end
	function img_table:width()
		if INC_all_loaded_imgs[imgnum] ~= "empty" then
			return pge.texture.width(INC_all_loaded_imgs[imgnum])
		end
	end
	function img_table:height()
		if INC_all_loaded_imgs[imgnum] ~= "empty" then
			return pge.texture.height(INC_all_loaded_imgs[imgnum])
		end
	end
	function img_table:save(path)
		if INC_all_loaded_imgs[imgnum] ~= "empty" then
			pge.texture.unswizzle(INC_all_loaded_imgs[imgnum])
			pge.texture.save(INC_all_loaded_imgs[imgnum], path)
			pge.texture.swizzle(INC_all_loaded_imgs[imgnum])
		end
	end
	function img_table:clear(color)
		if INC_all_loaded_imgs[imgnum] ~= "empty" then
			INC_drawing_enabled()
			if not color then 
				color = Color.new(0,0,0)
			end 
			local width = pge.texture.width(INC_all_loaded_imgs[imgnum])
			local height = pge.texture.height(INC_all_loaded_imgs[imgnum])
			INC_all_loaded_imgs[imgnum] = pge.texture.create(width,height)
			INC_unset_buffer()
			INC_move_buffer()
			INC_all_loaded_imgs[imgnum]:tovram()
			INC_all_loaded_imgs[imgnum]:settarget()
			pge.gfx.clearscreen(INC_all_loaded_colors[color.id])
			pge.gfx.swapbuffers()
			INC_all_loaded_imgs[imgnum]:toram()
		--[[pge.texture.unswizzle(INC_all_loaded_imgs[imgnum])
		for y = 1, pge.texture.height(INC_all_loaded_imgs[imgnum]) do
			for x = 1, pge.texture.width(INC_all_loaded_imgs[imgnum]) do
				pge.texture.pixel(INC_all_loaded_imgs[imgnum], x-1, y-1, INC_all_loaded_colors[color.id])
			end
		end
		pge.texture.swizzle(INC_all_loaded_imgs[imgnum])]]
		--[[INC_unset_buffer()
		INC_move_buffer()
		pge.texture.unswizzle(INC_all_loaded_imgs[imgnum])
		INC_all_loaded_imgs[imgnum]:tovram()
		INC_all_loaded_imgs[imgnum]:settarget()
		pge.gfx.clearscreen(INC_all_loaded_colors[color.id])
		INC_all_loaded_imgs[imgnum]:toram()
		pge.texture.swizzle(INC_all_loaded_imgs[imgnum])
		pge.gfx.swapbuffers()
		pge.gfx.swapbuffers()]]
		end
	end
	function img_table:blit(x,y,img,sourcex, sourcey, width, height, alpha)
		if INC_all_loaded_imgs[img.id] ~= "empty" and INC_all_loaded_imgs[imgnum] ~= "empty" then
			if not sourcex then 
				sourcex = 0
			end 
			if not sourcey then 
				sourcey = 0
			end 
			if not width then 
				width = INC_all_loaded_imgs[img.id]:width()
			end 
			if not height then 
				height = INC_all_loaded_imgs[img.id]:height()
			end 
			if not alpha then
				alpha = 255
			else
				alpha = 255
			end
			if not blit_done then blit_done = {} end
			do_blit = 1
			for i=1,#blit_done do
				if blit_done[i][1] == img.id and blit_done[i][2] == x and blit_done[i][3] == y then
					do_blit = 0
				break end
			end
			if do_blit == 1 then
				INC_drawing_enabled()
				INC_unset_buffer()
				INC_move_buffer()
				pge.texture.unswizzle(INC_all_loaded_imgs[imgnum])
				pge.texture.unswizzle(INC_all_loaded_imgs[img.id])
				INC_all_loaded_imgs[imgnum]:tovram()
				INC_all_loaded_imgs[imgnum]:settarget()
					INC_all_loaded_imgs[img.id]:activate()
					INC_all_loaded_imgs[img.id]:draw(x, y, width, height, sourcex, sourcex, width, height, 0, alpha)
				INC_all_loaded_imgs[imgnum]:toram()
			--[[for py = 1, pge.texture.height(INC_all_loaded_imgs[img.id]) do
				for px = 1, pge.texture.width(INC_all_loaded_imgs[img.id]) do
					pge.texture.pixel(INC_all_loaded_imgs[num], x+(px-1), y+(py-1), pge.texture.pixel(INC_all_loaded_imgs[img.id],px,py))
				end
			end]]
				pge.texture.swizzle(INC_all_loaded_imgs[imgnum])
				pge.texture.swizzle(INC_all_loaded_imgs[img.id])
				table.insert(blit_done, {img.id, x, y})
			end
		end
	end
	function img_table:drawLine(x0, y0, x1, y1, color)
		if INC_all_loaded_imgs[imgnum] ~= "empty" then
			if not drawLine_done then drawLine_done = {} end
			do_drawLine = 1
			for i=1,#drawLine_done do
				if drawLine_done[i][1] == x0 and drawLine_done[i][2] == y0 and drawLine_done[i][3] == x1 and drawLine_done[i][4] == y1 and drawLine_done[i][5] == color.id then
					do_drawLine = 0
				break end
			end
			if do_drawLine == 1 then
				INC_drawing_enabled()
				INC_unset_buffer()
				INC_move_buffer()
				pge.texture.unswizzle(INC_all_loaded_imgs[imgnum])
				INC_all_loaded_imgs[imgnum]:tovram()
				INC_all_loaded_imgs[imgnum]:settarget()
					pge.gfx.drawline(x0, y0, x1, y1, INC_all_loaded_colors[color.id])
				INC_all_loaded_imgs[imgnum]:toram()
			--[[local delta_x = x1-x0
			local delta_y = y1-y0
			local m = delta_y/delta_x
			for i = 1, delta_x do
				--if m*i == math.ceil(m*i) then
					pge.texture.pixel(INC_all_loaded_imgs[num],x0+(i-1),y0+m*i,INC_all_loaded_colors[color.id])
				--end
			end]]
				pge.texture.swizzle(INC_all_loaded_imgs[imgnum])
				table.insert(drawLine_done, {x0, y0, x1, y1, color.id})
			end
		end
	end
	function img_table:print(x,y,text,color)
		if INC_all_loaded_imgs[imgnum] ~= "empty" then
			if INC_printfont == 0 then
					INC_all_loaded_fonts[#INC_all_loaded_fonts+1] = pge.font.load(INC_font,INC_standard_fontsize, PGE_RAM)
					INC_printfont = #INC_all_loaded_fonts
			end
			if not print_done then print_done = {} end
			do_print = 1
			for i=1,#print_done do
				if print_done[i][1] == x and print_done[i][2] == y and print_done[i][3] == text and print_done[i][4] == color.id then
					do_print = 0
				break end
			end
			if do_print == 1 then
				INC_drawing_enabled()
				INC_unset_buffer()
				INC_move_buffer()
				pge.texture.unswizzle(INC_all_loaded_imgs[imgnum])
				INC_all_loaded_imgs[imgnum]:tovram()
				INC_all_loaded_imgs[imgnum]:settarget()
					pge.font.activate(INC_all_loaded_fonts[INC_printfont])
					pge.font.print(INC_all_loaded_fonts[INC_printfont], x, y, INC_all_loaded_colors[color.id], text)
				INC_all_loaded_imgs[imgnum]:toram()
				pge.texture.swizzle(INC_all_loaded_imgs[imgnum])
				table.insert(print_done, {x, y, text, color.id})
			end
		end
	end
	function img_table:fontPrint(font,x,y,text,color)
		if INC_all_loaded_imgs[imgnum] ~= "empty" then
			if not fontprint_done then fontprint_done = {} end
			do_fontprint = 1
			for i=1,#fontprint_done do
				if fontprint_done[i][1] == x and fontprint_done[i][2] == y and fontprint_done[i][3] == text and fontprint_done[i][4] == color.id and fontprint_done[i][5] == font.id then
					do_fontprint = 0
				break end
			end
			if do_fontprint == 1 then
				INC_drawing_enabled()
				INC_unset_buffer()
				INC_move_buffer()
				pge.texture.unswizzle(INC_all_loaded_imgs[imgnum])
				INC_all_loaded_imgs[imgnum]:tovram()
				INC_all_loaded_imgs[imgnum]:settarget()
					pge.font.activate(INC_all_loaded_fonts[font.id])
					pge.font.print(INC_all_loaded_fonts[font.id], x, y, INC_all_loaded_colors[color.id], text)
				INC_all_loaded_imgs[imgnum]:toram()
				pge.texture.swizzle(INC_all_loaded_imgs[imgnum])
				table.insert(fontprint_done, {x, y, text, color.id, font.id})
			end
		end
	end
	function img_table:fillRect(x, y, width, height, color)
		if INC_all_loaded_imgs[imgnum] ~= "empty" then
			if not fillRect_done then fillRect_done = {} end
			do_fillRect = 1
			for i=1,#fillRect_done do
				if fillRect_done[i][1] == x and fillRect_done[i][2] == y and fillRect_done[i][3] == width and fillRect_done[i][4] == height and fillRect_done[i][5] == color.id then
					do_fillRect = 0
				break end
			end
			if do_fillRect == 1 then
				INC_drawing_enabled()
				INC_unset_buffer()
				INC_move_buffer()
				pge.texture.unswizzle(INC_all_loaded_imgs[imgnum])
				INC_all_loaded_imgs[imgnum]:tovram()
				INC_all_loaded_imgs[imgnum]:settarget()
					pge.gfx.drawrect(x, y, width, height, INC_all_loaded_colors[color.id], 0)
				INC_all_loaded_imgs[imgnum]:toram()
			--[[for py = 1, height do
				for px = 1, width do
					pge.texture.pixel(INC_all_loaded_imgs[num],x+(px-1),y+(py-1),INC_all_loaded_colors[color.id])
				end
			end]]
				pge.texture.swizzle(INC_all_loaded_imgs[imgnum])
				table.insert(fillRect_done, {x, y, width, height, color.id})
			end
		end
	end
return img_table
end 

function Image.createEmpty(w,h)
	INC_newtex_w = w
	INC_newtex_h = h
return Image.load("INC_NEW_TEX")
end 

--##IO

--[[function io.open(file, mode)
	local file_table = {}
	if mode == "r" then
		mode = PGE_FILE_RDONLY
	elseif mode == "w" then
		mode = PGE_FILE_WRONLY
	elseif mode == "a" then
		mode = PGE_FILE_APPEND
	elseif mode == "a+" then
		mode = PGE_FILE_APPEND
	elseif mode == "r+" then
		mode = PGE_FILE_RDONLY
	elseif mode == "w+" then
		mode = PGE_FILE_WRONLY
	end
	table.insert(file_table, {handle = pge.file.open(file, mode)}
	--file functions
	function file_table:close()
		return pge.file.close(file_table.handle)
	end
	function file_table:seek(whence, offset)
		if not whence or whence == "cur" then
			whence = PGE_FILE_CUR
		elseif whence == "set" then
			whence = PGE_FILE_SET
		elseif whence == "end" then
			whence = PGE_FILE_END
		end
		if not offset then
			offset = 0
		end
		return pge.file.seek(file_table.handle, offset, whence)
	end
	function file_table:read(what)
		if not what then
		
		elseif what == "*n" then
		
		elseif what == "*a" then
		
		else
		
		end
	end
	return file_table
end

function io.lines()
	return true
end

function io.flush()
	return true
end

function io.close(handle)
	pge.file.close(handle)
end

function io.write(file, data)
	pge.file.write(file, data)
end]]

--##SYSTEM

function System.currentDirectory(path)
	if not path then
		return pge.dir.getcwd()
	else
		return pge.dir.chdir(path)
	end
end

function System.listDirectory(path)
	--[[if not path then path = "./" end
	if pge.dir.exists(path) then
		_dir = pge.dir.open(path)
		if _dir then
			_dircontents = pge.dir.read(_dir)
		end
		return _dircontents
	else
		return nil
	end]]
	if not path then path = "./" end
	--if pge.dir.exists(path) then
		_dir = pge.dir.open(path)
		return pge.dir.read(_dir)
	--else
	--	return nil
	--end
end

function System.createDirectory(path)
	return pge.dir.mkdir(path)
end

function System.removeDirectory(path)
	return pge.dir.rmdir(path)
end

function System.removeFile(file)
	return pge.file.remove(file)
end	

function System.rename(file, newname)
	return pge.file.rename(file, newname)
end

function System.nickName()
	return pge.utils.nickname()
end

function System.usbDiskModeActivate()
	return pge.usb.init(), pge.usb.activate()
end

function System.usbDiskModeDeactivate()
	return pge.usb.deactivate(), pge.usb.shutdown()
end

function System.Quit()
	pge.exit()
end







